package com.smartsystem.common.controller;

import com.smartsystem.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 * 提供系统健康检查和基本信息接口
 */
@RestController
@RequestMapping("/api/test")
@Tag(name = "测试接口", description = "🧪 系统健康检查、基本信息等测试相关接口")
public class TestController {

    /**
     * 简单测试接口
     * @return 简单的问候信息
     */
    @GetMapping("/hello")
    @Operation(summary = "Hello接口", description = "最简单的测试接口，返回问候信息")
    public Result<String> hello() {
        return Result.success("Hello! 智慧超市管理系统运行正常 🎉");
    }

    /**
     * 健康检查接口
     * @return 系统健康状态
     */
    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查系统是否正常运行")
    public Result<Map<String, Object>> health() {
        Map<String, Object> healthInfo = new HashMap<>();
        healthInfo.put("status", "UP");
        healthInfo.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        healthInfo.put("message", "智慧超市管理系统运行正常");
        healthInfo.put("version", "v1.0.0");
        
        return Result.success(healthInfo);
    }

    /**
     * 系统信息接口
     * @return 系统基本信息
     */
    @GetMapping("/info")
    @Operation(summary = "系统信息", description = "获取系统基本信息")
    public Result<Map<String, Object>> info() {
        Map<String, Object> systemInfo = new HashMap<>();
        
        // 系统信息
        systemInfo.put("applicationName", "智慧超市管理系统");
        systemInfo.put("version", "v1.0.0");
        systemInfo.put("description", "面向中小型超市的综合性管理平台");
        
        // 技术栈信息
        Map<String, String> techStack = new HashMap<>();
        techStack.put("framework", "Spring Boot 2.7.15");
        techStack.put("security", "Spring Security 6.2+");
        techStack.put("database", "MySQL 8.0+ & Redis 7.0+");
        techStack.put("orm", "MyBatis-Plus 3.5+");
        techStack.put("java", "Java 21");
        techStack.put("documentation", "Knife4j 4.1.0");
        systemInfo.put("techStack", techStack);
        
        // 运行时信息
        Runtime runtime = Runtime.getRuntime();
        Map<String, Object> runtimeInfo = new HashMap<>();
        runtimeInfo.put("javaVersion", System.getProperty("java.version"));
        runtimeInfo.put("osName", System.getProperty("os.name"));
        runtimeInfo.put("osVersion", System.getProperty("os.version"));
        runtimeInfo.put("totalMemory", runtime.totalMemory() / 1024 / 1024 + " MB");
        runtimeInfo.put("freeMemory", runtime.freeMemory() / 1024 / 1024 + " MB");
        runtimeInfo.put("maxMemory", runtime.maxMemory() / 1024 / 1024 + " MB");
        runtimeInfo.put("availableProcessors", runtime.availableProcessors());
        systemInfo.put("runtime", runtimeInfo);
        
        // 服务信息
        Map<String, String> serviceInfo = new HashMap<>();
        serviceInfo.put("serverPort", "8080");
        serviceInfo.put("contextPath", "/api");
        serviceInfo.put("apiDocUrl", "http://localhost:8080/api/doc.html");
        serviceInfo.put("druidMonitorUrl", "http://localhost:8080/api/druid");
        systemInfo.put("service", serviceInfo);
        
        systemInfo.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        return Result.success(systemInfo);
    }

    /**
     * 欢迎接口
     * @return 欢迎信息
     */
    @GetMapping("/welcome")
    @Operation(summary = "欢迎信息", description = "获取系统欢迎信息")
    public Result<Map<String, Object>> welcome() {
        Map<String, Object> welcomeInfo = new HashMap<>();
        welcomeInfo.put("message", "🎉 欢迎使用智慧超市管理系统！");
        welcomeInfo.put("description", "这是一个功能完善的超市管理解决方案");
        welcomeInfo.put("features", new String[]{
            "🔐 用户认证与权限管理",
            "📦 商品管理",
            "📊 库存管理", 
            "🛒 采购管理",
            "💰 销售管理",
            "💳 财务管理",
            "📈 报表统计"
        });
        welcomeInfo.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        return Result.success(welcomeInfo);
    }
}